<?php
//https://packagist.org/packages/chuyskywalker/rolling-curl
require 'vendor/autoload.php';
$rollingCurl = new \RollingCurl\RollingCurl();
for ($i = 1; $i <= 20; $i++) {
    // https://www.google.com/search?q=curl&start=10
    $rollingCurl->get('http://127.0.0.1:9501/test/slow_api?r=' . $i);
}

$results = array();

$start = microtime(true);
echo "Fetching..." . PHP_EOL;
$rollingCurl
    ->setCallback(function (\RollingCurl\Request $request, \RollingCurl\RollingCurl $rollingCurl) use (&$results) {
        $results[$request->getUrl()] = $request->getResponseText();

        // Clear list of completed requests and prune pending request queue to avoid memory growth
        $rollingCurl->clearCompleted();
        $rollingCurl->prunePendingRequestQueue();

        echo "Fetch complete for (" . $request->getUrl() . ")" . PHP_EOL;
    })
    ->setSimultaneousLimit(20)
    ->execute();;
echo "...done in " . (microtime(true) - $start) . PHP_EOL;

echo "All results: " . PHP_EOL;
print_r($results);
